package control;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.admin.AdminService;
import model.admin.ListVO;
import model.admin.NoticeBoardVO;
import model.admin.NoticeListVO;
import model.admin.OptionalServiceVO;
import model.member.MemberVO;
import model.qna.QnABoardVO;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AdminController extends MultiActionController{
private AdminService adminService;

public AdminController(){}

public AdminController(AdminService adminService){
	this.adminService=adminService;
}
//모든 멤버를 출력
public ModelAndView allMember(HttpServletRequest request,HttpServletResponse response){
	String pageNo=request.getParameter("pageNo");
	ListVO listVO=null;
	try {
		listVO = adminService.allMember(pageNo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("admin_member_list.admin","lvo",listVO);
}
//멤버 아이디를 가지고 멤버정보 보기
public ModelAndView findMemberById(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
	MemberVO member=null;
	try {
		member=adminService.findMemberById(mvo);
		System.out.println(member);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("admin_member_info.admin","mvo",member);
}
//멤버의 유저레벨을 변경
public ModelAndView updateUserLevel(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
	try {
		adminService.updateUserLevel(mvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("home");
}
//멤버 강제탈퇴시킴 member의 deleteMember로 대체됨
/*public ModelAndView removeMember(HttpServletRequest request,HttpServletResponse response,MemberVO mvo){
	try {
		adminService.removeMember(mvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("home");
}*/

//회원들의 서비스 신청내용을 관리
public ModelAndView requestManagement(HttpServletRequest request,HttpServletResponse response){
	List<OptionalServiceVO> list= null;
	try {
		list=adminService.requestManagement();
		System.out.println(list);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("admin_request_management.admin","list",list);
}
//서비스 승인절차를 위한 메서드
public ModelAndView requestConfirm(HttpServletRequest request,HttpServletResponse response,OptionalServiceVO osvo){
	try {
		osvo = adminService.requestConfirm(osvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("admin_request_confirm.admin","osvo",osvo);
}
//서비스 승인 메서드
public ModelAndView adminAllow(HttpServletRequest request,HttpServletResponse response,OptionalServiceVO osvo){
	try {
		adminService.adminAllow(osvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("admin_response_ok.admin");
}
//글쓰기를위한 메서드
public ModelAndView writeNotice(HttpServletRequest request , HttpServletResponse response,NoticeBoardVO nvo){
	HttpSession session=request.getSession(false);
	MemberVO mvo=(MemberVO)session.getAttribute("mvo");
	nvo.setMemberVO(mvo);
	   try {
		adminService.writeNotice(nvo);
		System.out.println(nvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   System.out.println(nvo);
	return new ModelAndView("notice/admin_notice_getContent.notice","nvo",nvo);
	//�۾��� �Ϸ����� ������ ���� return ��
}
//글쓰기폼으로 가기위한 메서드
public ModelAndView noticeWriteForm(HttpServletRequest request, HttpServletResponse response){
	return new ModelAndView("notice/admin_notice_write.notice");
}
//delete 를 하기위한 메서드
public ModelAndView deleteNoticeBoard(HttpServletRequest request, HttpServletResponse response){
	int no = Integer.parseInt(request.getParameter("no"));
	try {
		adminService.deleteNoticeBoard(no);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return noticeList(request, response);
}

//업데이트 하기위한 폼으로 가는 메서드
public ModelAndView updateNoticeBoardContentView(HttpServletRequest request, HttpServletResponse response,NoticeBoardVO nvo){
	
	try {
		System.out.println(nvo.getNoticeNO());
		nvo=adminService.getContent(nvo);
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("notice/admin_notice_updateform.notice","nvo",nvo);
}
//업데이트 하기위한 메서드
public ModelAndView updateNoticeBoardContent(HttpServletRequest request, HttpServletResponse response,NoticeBoardVO nvo){
	try {
		System.out.println(nvo);
		adminService.updateNoticeBoardContent(nvo);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	System.out.println("nvo 업데이트 완료 후..."+nvo);
	return new ModelAndView("notice/admin_notice_getContent.notice","nvo",nvo);
}
//컨텐츠를 눌렀을때 컨텐츠정보로 가기위한 폼
public ModelAndView getNotice(HttpServletRequest request , HttpServletResponse response,NoticeBoardVO nvo){
	try {
		/*String no = request.getParameter("noticeNo");
		System.out.println(no);*/
		System.out.println(nvo);
		 nvo =adminService.getNotice(nvo);
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("notice/admin_notice_getContent.notice","nvo",nvo);
}
//글정보들을 불러오는 리스트
public ModelAndView noticeList(HttpServletRequest request , HttpServletResponse response){
	
	String pageNo = request.getParameter("pageNo");
	NoticeListVO listVO =null;
	try {
		listVO = adminService.getNoticeList(pageNo);
		System.out.println(listVO);
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return new ModelAndView("notice/admin_notice_noticeboardlist.notice","lvo",listVO);
}

}
